package com.googlecode.moneyManager.spreadSheetReader.service;

import com.googlecode.moneyManager.model.Currency;
import com.googlecode.moneyManager.model.dto.InputSecurityDTO;
import com.googlecode.moneyManager.reader.DataValueProvider;
import com.googlecode.moneyManager.service.ReaderI;
import com.googlecode.moneyManager.service.SecurityValueService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author: Sergiy Doroshenko
 * Date: Sep 14, 2010
 * Time: 3:56:19 PM
 */
@Service
public class GoogleSpreadSheetReader implements ReaderI {
    private DataValueProvider dataValueProvider;
    private SecurityValueService securityValueService;
    private static Logger log = Logger.getLogger(GoogleSpreadSheetReader.class);
    
    @Autowired
    public void setDataValueProvider(DataValueProvider dataValueProvider) {
        this.dataValueProvider = dataValueProvider;
    }
    @Autowired
    public void setSecurityValueService(SecurityValueService securityValueService) {
        this.securityValueService = securityValueService;
    }

    @Override
    public boolean readAndSaveValues() {
        try {
            String baksPart = dataValueProvider.getPartAmount(Currency.USD);
            String evroPart = dataValueProvider.getPartAmount(Currency.EUR);
            String grivnaPart = dataValueProvider.getPartAmount(Currency.UAH);

            String baksRate = dataValueProvider.getRate(Currency.USD);
            String evroRate = dataValueProvider.getRate(Currency.EUR);


            String grivnaAll = dataValueProvider.getSumAmount(Currency.UAH);
            String baksAll = dataValueProvider.getSumAmount(Currency.USD);
            log.debug(baksAll);
            log.debug(grivnaAll);
            log.debug(baksPart);
            log.debug(grivnaPart);
            log.debug("evroPart=" + evroPart);
            log.debug("baksRate = " + baksRate);
            log.debug(evroRate);
            InputSecurityDTO inputSecurityDTO = new InputSecurityDTO();
            inputSecurityDTO.baksAll = baksAll;
            inputSecurityDTO.grivnaAll = grivnaAll;
            inputSecurityDTO.baksPart = baksPart;
            inputSecurityDTO.evroPart = evroPart;
            inputSecurityDTO.grivnaPart = grivnaPart;
            inputSecurityDTO.baksRate = baksRate;
            inputSecurityDTO.evroRate = evroRate;
            securityValueService.addValues(inputSecurityDTO);


        } catch(Exception ioe) {
            ioe.printStackTrace();
        }
        return false;
    }
}
